package cn.doctorpda.meeting.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import cn.doctorpda.meeting.R;
import cn.doctorpda.meeting.ui.recommend.MyFragment;
import cn.doctorpda.meeting.ui.recommend.RecommendFragment;
import cn.doctorpda.meeting.view.ColorTrackView;
import cn.doctorpda.meeting.view.PagerIndicatorView;

/**
 * Created by huangjinlong on 2016/5/13.
 */
public class MainFragment extends BaseFragment implements View.OnClickListener {

    private PagerIndicatorView mIndicator;
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private RelativeLayout mRecommend, mWillcheck, mData, mMy;
    private List<ColorTrackView> mColorTrackTab = new ArrayList<>();

    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        initView(view);
        initData();
        initEvent();
        return view;
    }

    private void initEvent() {
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mIndicator.scroll(position, positionOffset);
                if (positionOffset > 0) {
                    ColorTrackView left = mColorTrackTab.get(position);
                    ColorTrackView right = mColorTrackTab.get(position + 1);

                    left.setDirection(1);
                    right.setDirection(0);
                    left.setProgress(1 - positionOffset);
                    right.setProgress(positionOffset);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        fragments.add(RecommendFragment.newInstance());
        fragments.add(RecommendFragment.newInstance());
        fragments.add(RecommendFragment.newInstance());
        fragments.add(RecommendFragment.newInstance());
        mAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(1);
        ColorTrackView colorTrackView = (ColorTrackView) mRecommend.getChildAt(1);
        colorTrackView.setProgress(1);
    }

    private void initView(View view) {
        mIndicator = (PagerIndicatorView) view.findViewById(R.id.indicator);
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mRecommend = (RelativeLayout) view.findViewById(R.id.rl_recommend);
        mWillcheck = (RelativeLayout) view.findViewById(R.id.rl_willcheck);
        mData = (RelativeLayout) view.findViewById(R.id.rl_data);
        mMy = (RelativeLayout) view.findViewById(R.id.rl_my);
        mColorTrackTab.add((ColorTrackView) mRecommend.getChildAt(1));
        mColorTrackTab.add((ColorTrackView) mWillcheck.getChildAt(1));
        mColorTrackTab.add((ColorTrackView) mData.getChildAt(1));
        mColorTrackTab.add((ColorTrackView) mMy.getChildAt(1));
        mRecommend.setOnClickListener(this);
        mWillcheck.setOnClickListener(this);
        mData.setOnClickListener(this);
        mMy.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_recommend:
                changePager(0);
                break;
            case R.id.rl_willcheck:
                changePager(1);
                break;
            case R.id.rl_data:
                changePager(2);
                break;
            case R.id.rl_my:
                changePager(3);
                break;
        }
    }

    private void changePager(int position) {
        mIndicator.scroll(position, 0);
        mViewPager.setCurrentItem(position, false);
        initTrackProgress();
        mColorTrackTab.get(position).setProgress(1);
    }
    private void initTrackProgress(){
        mColorTrackTab.get(0).setProgress(0);
        mColorTrackTab.get(1).setProgress(0);
        mColorTrackTab.get(2).setProgress(0);
        mColorTrackTab.get(3).setProgress(0);
    }

}
